﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Dami;
public partial class admin_Modules_product_Edit : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnAddNew.Attributes.Add("onclick", "return OnSubmitClick();");
       
        if (!this.IsPostBack) {
            InitItem();
           
        }
    }
   
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        AddNew();
        
    }


    private void AddNew()
    {
        String strMess = "";
        int id = 0;
        int ID = -1;
        try
        {
            ID = Convert.ToInt32(Request.Params["ID"].ToString());
        }
        catch { }
        try
        {
            clsFileUpLoad fileUpload = new clsFileUpLoad();
            String strPath = "";
            if (String.IsNullOrEmpty(this.fulimageProduct.FileName))
            {
                strPath = Session["imageProduct"] == null ? "" : Session["imageProduct"].ToString();
            }
            else
            {
                fileUpload.DeleteFile(Session["imageProduct"].ToString());
                strPath = fileUpload.FileSaveImage("upload/product", this.fulimageProduct);
            }

            WebData db = new WebData();
            DataLibrary.tbl_Product product = new DataLibrary.tbl_Product();
            product.IdProduct = ID;
            product.ImageProduct = strPath;
            product.Path = "";
            product.OrderId = Convert.ToInt32(txtorderId.Text);
            product.IsActive = 1;
            product.Description = txtTieude.Text;
            id = db.tbl_Products.UpdateOnSubmit(product);
            
            DataLibrary.tbl_Product_Lang item = new DataLibrary.tbl_Product_Lang();
            if (Session["idProductLang"] != null)
            {
                item.IdProductLang = Convert.ToInt32(Session["idProductLang"].ToString());
            }
            item.IdLang =1;
            item.Description = txtDescription.Text;
            item.IdProduct = ID;
            item.IntroProduct = txtNoidung.Text;
            item.NameProduct = txtTieude.Text;
            item.Price = Convert.ToDecimal(txtPrice.Text);
            id = db.tbl_Product_Langs.UpdateOnSubmit(item);

        }
        catch (Exception err)
        {
            strMess = Message.Insert_err + "! vì lỗi " + err.Message.ToString();
        }
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + strMess + "');", true);
        if (id > 0)
            Response.Redirect(ViewState["URLReferrer"].ToString());

    }

    private void InitItem()
    {
        int ID=-1;
        try {
            ID = Convert.ToInt32(Request.Params["ID"].ToString());
        }
        catch { }
        ViewState["URLReferrer"] = Request.UrlReferrer.AbsoluteUri;
        WebData db=new WebData();

        String strSql = "select pro.idProduct,pro.imageProduct,lang.nameProduct,pro.orderId,lang.idProductLang,"
            + "lang.Description,lang.introProduct,lang.Price from tbl_Product pro,tbl_Product_Lang lang"
            + " where pro.idProduct=lang.idProduct and idLang=1 and pro.idProduct=@"+DataLibrary.tbl_Product.c_idProduct;

        DataTable tb = db.tbl_Products.GetDataTable(strSql, CommandType.Text,
            new SqlParameter("@"+DataLibrary.tbl_Product.c_idProduct,ID));

        if (tb.Rows.Count > 0) {
            txtDescription.Text = tb.Rows[0]["Description"].ToString();
            txtNoidung.Text = tb.Rows[0]["introProduct"].ToString();
            txtorderId.Text = tb.Rows[0]["orderId"].ToString();
            txtPrice.Text = tb.Rows[0]["Price"].ToString();
            txtTieude.Text = tb.Rows[0]["nameProduct"].ToString();
            Session["imageProduct"] = tb.Rows[0]["imageProduct"].ToString();
            Session["idProductLang"] = tb.Rows[0]["idProductLang"].ToString();
        }

    }
   
  
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(ViewState["URLReferrer"].ToString());
    }
}